package com.quanshu.exchange.ui.gui.message.chat.row.viewholder;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.baselibrary.util.glide.GlideUtils;
import com.hyphenate.chat.EMFileMessageBody;
import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMMessage;
import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.gui.message.chat.row.BaseChatRowViewHolder;
import com.quanshu.exchange.ui.gui.message.chat.row.ChatImageUtil;

import java.io.File;
import java.util.List;

// 接收图片消息ViewHolder
public class ImageReceiveViewHolder extends BaseChatRowViewHolder {
    ImageView image;
    LinearLayout loading;
    List<EMMessage> allMessages;

    public ImageReceiveViewHolder(Context context, View view, List<EMMessage> allMessages) {
        super(context, view);
        image = view.findViewById(R.id.chat_image);
        loading = view.findViewById(R.id.ll_loading);
        this.allMessages = allMessages;
    }

    @Override
    public void setViewInfo(EMMessage chatMessage, EMMessage preMessage, String groupOwner) {
        super.setViewInfo(chatMessage, preMessage, groupOwner);
        // 设置内容
        EMImageMessageBody imgBody = (EMImageMessageBody) chatMessage.getBody();
        if (imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING ||
                imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) {
            loading.setVisibility(View.VISIBLE);
            GlideUtils.setImageViewNoThumb(context, null, R.drawable.default_img, image);
        } else {
            loading.setVisibility(View.GONE);
            String thumbPath = imgBody.thumbnailLocalPath();
            if (!new File(thumbPath).exists()) {
                // to make it compatible with thumbnail received in previous version
                thumbPath = ChatImageUtil.getThumbnailImagePath(imgBody.getLocalUrl());
            }
            ChatImageUtil.showFromImageView(imgBody, thumbPath, image, imgBody.getLocalUrl(), chatMessage, context);
        }
    }
}